export const WebScoket2 = {
    data() {
    return {
        checkCode:'',
      };
    },
    methods: {
      initWebSocket4() {
        this.websock4 = null;
        //判断当前浏览器是否支持WebSocket
        if ("WebSocket" in window) {
          let url =  process.env.VUE_APP_BASE_API.slice(4)
         let webSocketUrl = "ws" + url+'/webSocket/'
          this.websock4 = new WebSocket(webSocketUrl + this.checkCode); //新建一个webstock对象
          this.websock4.onopen = this.websocketonopen4; //连接成功建立的回调方法
          this.websock4.onmessage = this.websocketonmessage4; //接收到消息的回调方法
          this.websock4.onerror = this.websocketonerror4; //连接发生错误的回调方法
          this.websock4.onclose = this.websocketclose4; //连接关闭的回调方法
        } else {
          alert("当前浏览器 Not support websocket");
        }
      },
      websocketonopen4() {
        console.log("---数据上传连接建立成功---");
      },
      websocketonerror4() {
        //连接建立失败重连
        this.initWebSocket4();
      },
      websocketonmessage4(e) {
        //数据接收
        console.log(e.data);
      },
      websocketsend(Data) {
        //数据发送
        this.websock4.send(Data);
      },
      onbeforeunload() {
        this.websocket4.close();
      },
  
      websocketclose4(e) {
        //关闭
        console.log("断开连接", e);
        this.websock4.close();
      },
      //随机数
      createCode() {
        let code = "";
        var codeLength = 6;
        var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
        for (var i = 0; i < codeLength; i++) {
          var index = Math.floor(Math.random() * 10);
          code += random[index];
        }
        this.checkCode = code;
      }
    },
    created() {
    this.createCode();
    }
  };
  